/**
 *
 */
(function (factory) {
    if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
        factory(exports);
    } else if (typeof define === "function" && define["amd"]) {
        define(["exports"], factory);
    } else {
        window.lego = window.lego || {};
        factory(lego.tool = lego.tool || {});
    }
}(function (exports) {
    var isBusy = false, tempUrl = '';
    var restClient = function (p) {
        if (isBusy && tempUrl == p.url) return;
        var url = p.url;
        if (isNotNull(p.param)) {
            url = url + "?" + p.param;
        }
        var data = null;
        if (isNotNull(p.data)) {
            data = p.data;
        }

        isBusy = true;
        tempUrl = url;

        return $.ajax({
            type: "POST",
            contentType: 'application/json;charset=UTF-8',
            url: url,
            data: data || '{}',
            beforeSend: function (xhr) {
                // xhr.setRequestHeader('X-AUTH-TOKEN', 'MyToken');
                p.beforeFun && p.beforeFun(xhr);
            },
            complete: function () {
                isBusy = false;
                tempUrl = '';
            },
            success: function (data, textStatus, request) {
                // if (request.getResponseHeader("X-AUTH-TOKEN")) {}
                p.successFun && p.successFun(data);
            },
            error: function (xhr, txtStatus, error) {
                p.errorFun && p.errorFun();
            }
        });
    };

    var isNotNull = function (value) {
        if (value) {
            return true;
        } else {
            return false;
        }
    };

    /**
     * 根据key获取url中的参数
     *
     * @param key 参数key
     * @param url 为空时表示window.location
     * @returns {null}
     */
    var getUrlParam = function (key, url) {
        var searchUrl = '';
        if (isNotNull(url)) {
            searchUrl = url.split('?')[1];
        } else {
            searchUrl = window.location.search.substr(1);
        }
        if (!isNotNull(searchUrl)) {
            return null;
        }
        var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
        var r = searchUrl.match(reg);
        if (r && $.isArray(r) && r[2]) return unescape(r[2]);
        return null;
    };

    var generateUUID = function () {
        var d = new Date().getTime();
        var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            var r = (d + Math.random() * 16) % 16 | 0;
            d = Math.floor(d / 16);
            return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
        });
        return uuid;
    };

    var findInArray = function (array, id, customKeyOfId) {
        var result;
        $.each(array, function (index, item) {
            if (id == item[customKeyOfId || 'id']) {
                result = item;
                return false;
            }
        });
        return result;
    };

    exports.restClient = restClient;
    exports.isNotNull = isNotNull;
    exports.getUrlParam = getUrlParam;
    exports.generateUUID = generateUUID;
    exports.findInArray = findInArray;
}));